package com.wswy.wzcx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.aanewUi.fragment.InspectionServiceFragmentNew;
import com.wswy.wzcx.model.home.RPaths;
import com.wswy.wzcx.model.license.UserCarInfo;
import com.wswy.wzcx.model.wzdb.WzdbOrderInfo;
import com.wswy.wzcx.module.GlobalConfigManager;
import com.wswy.wzcx.ui.Constants;
import com.wswy.wzcx.ui.car.result.HistoryFinesFragment;
import com.wswy.wzcx.ui.card.MyLicenseFragment;
import com.wswy.wzcx.ui.coupon.ChooseCouponFragment;
import com.wswy.wzcx.ui.coupon.CouponListFragment;
import com.wswy.wzcx.ui.data.JZCFResult;
import com.wswy.wzcx.ui.fragment.FKDJResultFragment;
import com.wswy.wzcx.ui.fragment.JZCFFragment;
import com.wswy.wzcx.ui.fragment.JZCFResultFragment;
import com.wswy.wzcx.ui.fragment.PrivacyFragment;
import com.wswy.wzcx.ui.fragment.WZDBFragment;
import com.wswy.wzcx.ui.fragment.WZDBSubmitFragment;
import com.wswy.wzcx.ui.main.community.CommunityFragment;
import com.wswy.wzcx.ui.main.more.AllModulesFragment;
import com.wswy.wzcx.ui.module.feedback.AddFeedbackFragment;
import com.wswy.wzcx.ui.module.feedback.MyFeedbackFragment;
import com.wswy.wzcx.ui.module.lbs.Lbs;
import com.wswy.wzcx.ui.module.lbs.LbsMasterFragment;
import com.wswy.wzcx.ui.module.oilprice.OilTrendFragment;
import com.wswy.wzcx.ui.module.oilprice.TodayOilPriceFragment;
import com.wswy.wzcx.ui.module.tels.CommonNumbersFragment;
import com.wswy.wzcx.ui.njdb.InspectionServiceFragment;
import com.wswy.wzcx.ui.njdb.NjdbOrderFragment;
import com.wswy.wzcx.ui.njdb.NjdbPreOrderFragment;
import com.wswy.wzcx.ui.njdb.NjdbStepFragment;
import com.wswy.wzcx.ui.njdb.NjdbUploadFragment;
import com.wswy.wzcx.ui.pay.KfServiceFragment;
import com.wswy.wzcx.ui.pay.PayOrderFragment;
import com.wswy.wzcx.ui.pay.PaySuccessFragment;
import com.wswy.wzcx.ui.wzdb.UserFaceFragment;
import com.wswy.wzcx.ui.wzdb.WzdbPickFragment;

/**
 * Created by wangduheng26 on 2018/3/5.
 * wangduheng26@gmail.com
 */

public class ModuleActivity extends IFragmentActivity implements Toolbar.OnMenuItemClickListener {

    public static Intent getTargetIntent(Context context, String target) {
        Intent intent = new Intent(context, ModuleActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        return intent;
    }

    public static Intent getTargetIntent(Context context, String target, Bundle bundle) {
        Intent intent = new Intent(context, ModuleActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        intent.putExtra(Constants.EXTRA_BUNDLE, bundle);
        return intent;
    }

    public static Intent getTargetIntent(Context context, String target, boolean hideTitle) {
        Intent intent = new Intent(context, ModuleActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        intent.putExtra(EXTRA_HIDE_TITLE, hideTitle);
        return intent;
    }

    public static void startTarget(Context context, String target) {
        Intent intent = new Intent(context, ModuleActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        context.startActivity(intent);
    }

    @Override
    protected BaseFragment getFragmentFromExtra(String stringExtra) {
        BaseFragment fragment = null;
        Intent intent = getIntent();
        UserCarInfo userCarInfo = getCurrentUserCar();

        Bundle bundle = getCurrentArgs();

        switch (stringExtra) {
            case JZCFFragment.TAG:
                fragment = JZCFFragment.newInstance();
                break;
            case JZCFResultFragment.TAG:
                JZCFResult mode = intent.getParcelableExtra("data");
                fragment = JZCFResultFragment.newInstance(mode);
                break;
            case WZDBFragment.TAG:
                fragment = WZDBFragment.newInstance();
                break;
            case WzdbPickFragment.TAG:

                fragment = WzdbPickFragment.newInstance(intent.getExtras());
                break;
            case WZDBSubmitFragment.TAG:
                WzdbOrderInfo modes = intent.getParcelableExtra("orderInfo");
                if (modes != null) {
                    fragment = WZDBSubmitFragment.newInstance(modes);
                } else {
                    String orderNo = intent.getStringExtra("orderNo");
                    fragment = WZDBSubmitFragment.newInstance(orderNo);
                }

                break;
            case FKDJResultFragment.TAG:
                boolean res = intent.getBooleanExtra("data", false);
                fragment = FKDJResultFragment.Companion.newInstance(res);
                break;
            case CommunityFragment.TAG:
                fragment = CommunityFragment.newInstance();
                break;
            case ChooseCouponFragment.TAG:
                fragment = ChooseCouponFragment.newInstance(intent.getExtras());
                break;
            case CouponListFragment
                    .TAG:
                fragment = CouponListFragment.newInstance();
                break;
            case RPaths
                    .OIL_PRICES:
                fragment = new TodayOilPriceFragment();
                break;
            case RPaths.OIL_TREND:
                Bundle vos = null;
                if (intent != null && (vos = intent.getBundleExtra(Constants.EXTRA_DATA)) != null) {
                    fragment = OilTrendFragment.newInstance(vos);
                }
                break;
            case RPaths.LBS_CAR_PARK:
                fragment = LbsMasterFragment.newInstance(Lbs.INSTANCE.getTYPE_CAR_PARK().getTypeCode());
                break;
            case RPaths.LBS_GAS_STATION:
                fragment = LbsMasterFragment.newInstance(Lbs.INSTANCE.getTYPE_GAS().getTypeCode());
                break;
            case RPaths.LBS_CAR_WASH:
                fragment = LbsMasterFragment.newInstance(Lbs.INSTANCE.getTYPE_CAR_WASH().getTypeCode());
                break;
            case RPaths.LBS_CAR_4S:
                fragment = LbsMasterFragment.newInstance(Lbs.INSTANCE.getTYPE_CAR_4S().getTypeCode());
                break;
            case RPaths.ALL_MODULES:
                fragment = new AllModulesFragment();
                break;
            case RPaths.COMMON_NUMBERS:
                fragment = new CommonNumbersFragment();
                break;
            case RPaths.ADD_FEEDBACK:
                fragment = new AddFeedbackFragment();
                break;
            case RPaths.MY_FEEDBACK:
                fragment = new MyFeedbackFragment();
                break;
            case RPaths.HISTORY_FINE:
                if (userCarInfo != null) {
                    fragment = HistoryFinesFragment.newInstance(userCarInfo);
                }
                break;
            case RPaths.MY_LICENSE:
                fragment = new MyLicenseFragment();
                break;
            case RPaths.NJDB:
                fragment = InspectionServiceFragment.newInstance(bundle);
                //fragment = new InspectionServiceFragmentNew();
                break;
            case NjdbPreOrderFragment.TAG:
                if (bundle != null) {
                    fragment = NjdbPreOrderFragment.newInstance(bundle);
                }

                break;
            case NjdbStepFragment
                    .TAG:
                if (bundle != null) {
                    fragment = NjdbStepFragment.newInstance(bundle);
                }

                break;
//            case NjdbUploadFragment
//                    .TAG:
//            if (bundle != null){
//                fragment = NjdbUploadFragment.newInstance(bundle);
//            }
//                break;
            case NjdbOrderFragment
                    .TAG:
                fragment = new NjdbOrderFragment();
                fragment.setArguments(bundle);
                break;
            case RPaths.PAY_ORDER:
                if (bundle != null) {
                    fragment = PayOrderFragment.newInstance();
                    fragment.setArguments(bundle);
                }
                break;
            case UserFaceFragment.TAG:
                fragment = new UserFaceFragment();
                fragment.setArguments(bundle);
                break;
            case PaySuccessFragment.TAG:
                fragment = new PaySuccessFragment();
                fragment.setArguments(bundle);
                break;
            case RPaths.KF_WECHAT:
                fragment = new KfServiceFragment();
                fragment.setArguments(bundle);
                break;
            case RPaths.SETTINGS_PRIVACY:
                fragment = new PrivacyFragment();
                break;
            default:
                fragment = GlobalConfigManager
                        .getInstance().getFlavorConfigure().createFragment(stringExtra, intent);
                break;
        }
        return fragment;
    }

    private UserCarInfo getCurrentUserCar() {
        Intent intent = getIntent();
        UserCarInfo userCar = null;
        if (intent != null && intent.hasExtra(Constants.EXTRA_DATA)) {
            Object data = intent.getParcelableExtra(Constants.EXTRA_DATA);
            if (data instanceof UserCarInfo) {
                userCar = (UserCarInfo) data;
            }
        }
        return userCar;
    }

    private Bundle getCurrentArgs() {
        Intent intent = getIntent();
        Bundle bundle = null;
        if (intent != null && intent.hasExtra(Constants.EXTRA_BUNDLE)) {
            bundle = intent.getParcelableExtra(Constants.EXTRA_BUNDLE);
        }
        return bundle;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void configToolBar() {
        super.configToolBar();
        toolbar.setOnMenuItemClickListener(this);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowHomeEnabled(true);
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }
}
